<template>
  <div class="wrapper">
    <header>
      <div class="left">
        <img src="@/assets/1.png" alt="" />
        <span>
          {{ title }}
        </span>
      </div>
      <el-button type="info" @click="exit">退出</el-button>
    </header>
    <section>
      <aside>
        <el-menu
          class="menu"
          router
          background-color="#333744"
          text-color="#fff"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menus"
            :key="item.id"
          >
            <template slot="title">
              {{ item.authName }}
            </template>
            <el-menu-item-group>
              <el-menu-item
                :index="child.path"
                v-for="child in item.children"
                :key="child.id"
              >
                {{ child.authName }}
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </aside>
      <main>
        <el-breadcrumb v-if="parent" separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{ parent }}</el-breadcrumb-item>
          <el-breadcrumb-item>{{ child }}</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="card">
          <router-view></router-view>
        </div>
      </main>
    </section>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  name: "HomeLayout",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      /**
       * 父菜单
       */
      parent: "",
      /**
       * 子菜单
       */
      child: "",
    };
  },
  created() {
    this.findNames();
  },
  updated() {
    this.findNames();
  },
  methods: {
    /**
     * 退出
     */
    async exit() {
      try {
        await this.$confirm("您确定要退出吗");
        this.$store.dispatch("exit");
      } catch (eror) {}
    },
    /**
     * 根据路径查找当前页面的名称
     */
    findNames() {
      const path = this.$route.path.replace("/", "");
      for (const parent of this.menus) {
        for (const child of parent.children) {
          if (child.path === path) {
            this.parent = parent.authName;
            this.child = child.authName;
            return;
          }
        }
      }
    },
  },
  computed: {
    ...mapState(["menus"]),
  },
};
</script>
<style scoped lang="scss">
.wrapper {
  height: 100%;
  display: flex;
  flex-direction: column;

  header {
    height: 60px;
    background-color: #373d41;
    flex-shrink: 0; //缩小比例  0==不缩放  1==共同缩放.....
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #eaedf1;
    font-size: 20px;

    div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }
    }

    button {
      margin-right: 20px;
    }
  }

  section {
    flex: 1;
    display: flex;
    overflow: hidden;

    aside {
      width: 200px;
      background-color: #333744;
      height: 100%;
      overflow: auto;

      .menu {
        border: none;
      }
    }

    main {
      max-height: 100%;
      flex: 1;
      overflow: auto;
      padding: 20px;
      background-color: #eaedf1;

      ::v-deep .el-breadcrumb {
        font-size: 12px;
      }

      .card {
        margin-top: 15px;
        border-radius: 4px;
        background-color: white;
        border: 1px solid #ebeef5;
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15);
        padding: 20px;
      }
    }
  }
}
</style>
